package cn.wolfcode.web.controller;

import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.page.JsonPage;
import cn.wolfcode.ssm.service.IEmployeeService;
import cn.wolfcode.ssm.service.IPermissionService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.util.Password;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private IEmployeeService employeeService;

    // 判断登陆
    @RequestMapping("/login.do")
    @ResponseBody
    public JsonPage login(String username, String password){
        try {
            // new 一个令牌
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            // 利用shiro进行登陆.log就是登陆
            SecurityUtils.getSubject().login(token);
            return new JsonPage();
        } catch (UnknownAccountException e){
            e.printStackTrace();
            return new JsonPage(false,"账号输入有误");
        } catch (IncorrectCredentialsException e){
            e.printStackTrace();
            return new JsonPage(false,"密码输入有误");
        }catch (DisabledAccountException e){
            e.printStackTrace();
            return new JsonPage(false,"该账号被冻结,无法登陆");
        }
        catch (Exception e){
            e.printStackTrace();
            return new JsonPage(false,"登陆失败");
        }
    }


    // 不用了
//    // 注销实现
//    @RequestMapping("/loginOut")
//    public String  loginOut(HttpSession session){
//        // 删除session
//        session.invalidate();
//        // 跳转回登陆界面
//        return "redirect:/login.html";
//    }





    @RequestMapping("/updatePwd")
    @ResponseBody
    public JsonPage updatePwd(String oldPassword,String newPassword){
        // 利用shiro拿到登陆对象
        Employee employee = (Employee)SecurityUtils.getSubject().getPrincipal();
        // 拿到对应人的密码和传上来的就密码做对比
        String password = employeeService.contrastPassword(employee.getId());
        // 把老密码加密
        Md5Hash md5Hash = new Md5Hash(oldPassword, employee.getName());
        // 如果输入的老密码正确
        if ((md5Hash.toString()).equals(password)){
            // 把新密码加密
            Md5Hash md5Hash1 = new Md5Hash(newPassword, employee.getName());
            // 就执行修改密码方法
            employeeService.updatePassword(employee.getId(),md5Hash1.toString());
         return new JsonPage();
        }
        return new JsonPage(false,"原密码输入错误");

    }

    // 管理员重置密码
    @RequestMapping("/resetPassword")
    @ResponseBody
    public JsonPage resetPwd(String newPassword,Long userId,String name){
        // 管理员传上来的新密码和对应用户的Id(法相一直拿不到,可能是前端传过来是String类型,Sql语句需要的是Long类型,就改用name来做判断条件)
        System.out.println(userId);
        System.out.println(newPassword);
        Md5Hash md5Hash = new Md5Hash(newPassword,name);
        employeeService.resetPwd(md5Hash.toString(),userId);
        return new JsonPage();
    }


}
